/*
 * Copyright (C) 2004, Velocityme Partnership
 * ShowTreeAction.java
 *
 * Created on 12 September 2004, 12:12
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.Node;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.session.TreeLocal;
import com.velocityme.www.utility.NodeUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class ShowTreeAction extends DispatchAction {

    private Map<Class,String> classForwardMap;

    /** Creates a new instance of ShowTreeAction */
    public ShowTreeAction() {
        classForwardMap = new HashMap<Class,String>();
        classForwardMap.put(com.velocityme.entity.Department.class, "ACTION_SHOW_DEPARTMENT_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Organisation.class, "ACTION_SHOW_ORGANISATION_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Person.class, "ACTION_SHOW_PERSON_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Group.class, "ACTION_SHOW_GROUP_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Role.class, "ACTION_SHOW_ROLE_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Sprint.class, "ACTION_SHOW_SPRINT_SUCCESS");
        classForwardMap.put(com.velocityme.entity.State.class, "ACTION_SHOW_STATE_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Status.class, "ACTION_SHOW_STATUS_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Action.class, "ACTION_SHOW_ACTION_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Anchor.class, "ACTION_SHOW_ANCHOR_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Issue.class, "ACTION_SHOW_ISSUE_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Project.class, "ACTION_SHOW_PROJECT_SUCCESS");
        classForwardMap.put(com.velocityme.entity.TaskStateMachine.class, "ACTION_SHOW_TASKSTATEMACHINE_SUCCESS");
        classForwardMap.put(com.velocityme.entity.User.class, "ACTION_SHOW_USER_SUCCESS");
        classForwardMap.put(com.velocityme.entity.SystemNode.class, "ACTION_SHOW_SYSTEMNODE_SUCCESS");
        classForwardMap.put(com.velocityme.entity.Directory.class, "ACTION_SHOW_DIRECTORY_SUCCESS");
        classForwardMap.put(com.velocityme.entity.LicenceManagement.class, "ACTION_SHOW_LICENCEMANAGEMENT_SUCCESS");
        classForwardMap.put(com.velocityme.entity.SystemProperties.class, "ACTION_SHOW_SYSTEMPROPERTY_SUCCESS");
        classForwardMap.put(com.velocityme.entity.ContactDetailTypes.class, "ACTION_SHOW_CONTACTDETAILTYPES_SUCCESS");
        classForwardMap.put(com.velocityme.entity.LoginHistory.class, "ACTION_SHOW_LOGINHISTORY_SUCCESS");
    }
    
    public ActionForward home(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        // remove some critical attributes
        session.removeAttribute("nodeId");
        session.removeAttribute("nodeValue");
        session.removeAttribute("tabValues");
        session.removeAttribute("menuButtonValues");
        session.removeAttribute("buttonValues");
        session.removeAttribute("selectedTabValue");
        session.setAttribute("ancestorValues", lookupTreeBean().getVisibleAncestorNodes(key, null));
        session.setAttribute("menuDTO", NodeUtil.getInstance().getMenu(key, null, null, false, false, false, false));
                
        ShowNodeAction.setupChildrenDTOs(session, null);
        session.setAttribute("nodeId", new Long(0));
        session.setAttribute("tabValues", new String[]{});
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward node(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = null;
        try {
            key = (KeyLocal)session.getAttribute("sessionKey");
        }
        catch(ClassCastException e) {
            // this means that we are not currently logged in so ensure that we do
            key = null;
        }
        if (key == null) {
            session.setAttribute("nodeId", nodeId);
            return mapping.findForward("ACTION_LOGIN");
        }
        NodeLocal nodeLocal = lookupNodeBean();
        Node node = nodeLocal.findNode(key, nodeId);
        ShowNodeAction.setupTreeDisplay(session, node, node.getClass().getSimpleName());
        if (classForwardMap.containsKey(node.getClass())) {
            return mapping.findForward(classForwardMap.get(node.getClass()));
        }
        request.setAttribute("selectedTab", "Node");
        return mapping.findForward("ACTION_SHOW_NODE_SUCCESS");   
    }

    private TreeLocal lookupTreeBean() {
        try {
            Context c = new InitialContext();
            return (TreeLocal) c.lookup("java:comp/env/TreeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
